<template>
  <div class="forum-container">
    <div class="post-form card">
      <div class="form-row">
        <input v-model="newPostTitle" placeholder="请输入标题" class="post-input title-input" />
      </div>
      <div class="form-row">
        <textarea v-model="newPostContent" placeholder="分享你的想法..." class="post-textarea content-textarea"></textarea>
      </div>
      <div class="form-row post-actions">
        <div class="image-upload-section">
          <label for="image-upload" class="upload-button">插入图片</label>
          <input type="file" id="image-upload" multiple @change="handleImageUpload" accept="image/*" style="display: none;" />
          <span class="file-name">{{ selectedImageNames }}</span>
        </div>
        <button @click="handleCreatePost" class="submit-button">发布</button>
      </div>
    </div>

    <div class="posts-list">
      <div class="post-item card" v-for="post in posts" :key="post.id">
        <h3 class="post-title">{{ post.title }}</h3>
        <p class="post-content">{{ post.content }}</p>
        <div class="post-images" v-if="post.imageUrls && post.imageUrls.length > 0">
          <div class="preview-image-item" v-for="imageUrl in post.imageUrls" :key="imageUrl">
            <img :src="imageUrl" alt="Post Image" class="post-image" />
          </div>
        </div>
        <div class="post-meta">
          <span class="post-author">作者: {{ post.username }}</span>
          <span class="post-time">{{ formatDate(post.createTime) }}</span>
        </div>
        <div class="comments-section">
          <div class="comment-item" v-for="comment in post.comments" :key="comment.id">
            <p class="comment-content">{{ comment.content }} - <span class="comment-author">{{ comment.username }}</span></p>
          </div>
          <div class="add-comment">
            <input v-model="newComment" placeholder="添加评论..." class="comment-input" />
            <button @click="handleAddComment(post.id)" class="comment-button">评论</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getPosts, createPost, addComment } from '@/api/post';
import { formatDate } from '@/utils/date';
import { error, errorAlert } from '@/utils/alert';

const posts = ref([]);
const newPostTitle = ref('');
const newPostContent = ref('');
const newComment = ref('');
const selectedImages = ref([]);
const selectedImageNames = ref('未选择文件');

// 页面加载时获取帖子列表
onMounted(async () => {
  try {
    const response = await getPosts();
    posts.value = response.data;
  } catch (error) {
    console.error('获取帖子失败:', error);
    error('获取帖子失败，请重试');
  }
});

// 发布新帖子
const handleCreatePost = async () => {
  if (!newPostTitle.value.trim()) {
    error('标题不能为空');
    return;
  }
  if (!newPostContent.value.trim()) {
    error('内容不能为空');
    return;
  }

  try {
    const formData = new FormData();
    formData.append('title', newPostTitle.value);
    formData.append('content', newPostContent.value);
    selectedImages.value.forEach(image => {
      formData.append('images', image);
    });

    await createPost(formData);
    // 发布成功后重新获取帖子列表
    const response = await getPosts();
    posts.value = response.data;
    newPostTitle.value = '';
    newPostContent.value = '';
    selectedImages.value = [];
    selectedImageNames.value = '未选择文件';
  } catch (error) {
    console.error('发布帖子失败:', error);
    error('发布帖子失败，请重试');
  }
};

// 处理图片上传
const handleImageUpload = (event) => {
  selectedImages.value = Array.from(event.target.files);
  if (selectedImages.value.length > 0) {
    selectedImageNames.value = selectedImages.value.map(file => file.name).join(', ');
  } else {
    selectedImageNames.value = '未选择文件';
  }
};

// 添加评论
const handleAddComment = async (postId) => {
  if (!newComment.value.trim()) {
    error('评论不能为空');
    return;
  }

  try {
    await addComment(postId, newComment.value);
    // 评论成功后重新获取帖子详情或整个列表
    const response = await getPosts();
    posts.value = response.data;
    newComment.value = '';
  } catch (error) {
    console.error('添加评论失败:', error);
    error('添加评论失败，请重试');
  }
};
</script>

<style scoped>
.forum-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  background-color: #f0f2f5;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.post-form {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 25px;
}

.form-row {
  margin-bottom: 18px;
}

.post-input.title-input {
  font-size: 18px;
  font-weight: bold;
  padding: 14px 16px;
  border: 1.5px solid #e0e0e0;
  border-radius: 7px;
  background: #fafbfc;
  transition: border-color 0.2s;
}

.post-input.title-input:focus {
  border-color: #409eff;
  background: #fff;
}

.post-textarea.content-textarea {
  min-height: 110px;
  padding: 14px 16px;
  border: 1.5px solid #e0e0e0;
  border-radius: 7px;
  background: #fafbfc;
  font-size: 16px;
  transition: border-color 0.2s;
}

.post-textarea.content-textarea:focus {
  border-color: #409eff;
  background: #fff;
}

.post-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0;
}

.image-upload-section {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #f5f7fa;
  border-radius: 6px;
  padding: 6px 12px;
  border: 1px dashed #bfcbd9;
}

.upload-button {
  padding: 8px 15px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.upload-button:hover {
  background-color: #66b1ff;
}

.file-name {
  font-size: 14px;
  color: #666;
  max-width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.submit-button {
  padding: 10px 25px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s ease;
}

.submit-button:hover {
  background-color: #66b1ff;
}

.post-input {
  width: 100%;
  padding: 10px;
  border: 1px solid #e0e0e0;
  border-radius: 5px;
  font-size: 16px;
  margin-bottom: 10px;
}

.post-input:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.2);
}

.post-textarea {
  width: 100%;
  height: 120px;
  padding: 10px;
  border: 1px solid #e0e0e0;
  border-radius: 5px;
  font-size: 16px;
  resize: vertical;
  margin-bottom: 10px;
}

.post-textarea:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.2);
}

.image-upload-section {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.upload-button {
  padding: 8px 15px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s ease;
  margin-right: 10px;
}

.upload-button:hover {
  background-color: #66b1ff;
}

.file-name {
  font-size: 14px;
  color: #666;
}

.submit-button {
  align-self: flex-end;
  padding: 10px 20px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s ease;
}

.submit-button:hover {
  background-color: #66b1ff;
}

.posts-list {
  margin-top: 20px;
}

.post-item {
  /* 继承 .card 样式 */
}

.post-title {
  font-size: 20px;
  color: #333;
  margin-bottom: 10px;
  border-bottom: 1px solid #eee;
  padding-bottom: 8px;
}

.post-content {
  font-size: 16px;
  color: #555;
  line-height: 1.6;
  margin-bottom: 15px;
}

.post-images {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
}

.preview-image-item {
  width: 100px;
  height: 100px;
  overflow: hidden;
  border-radius: 5px;
  border: 1px solid #eee;
}

.post-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.post-meta {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: #999;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed #eee;
}

.post-author,
.post-time {
  margin-right: 15px;
}

.comments-section {
  margin-top: 20px;
  padding-top: 15px;
  border-top: 1px solid #f0f0f0;
}

.comment-item {
  background-color: #f9f9f9;
  border-radius: 5px;
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid #eee;
}

.comment-content {
  font-size: 14px;
  color: #666;
  margin-bottom: 5px;
}

.comment-author {
  font-weight: bold;
  color: #409eff;
}

.add-comment {
  display: flex;
  margin-top: 15px;
}

.comment-input {
  flex-grow: 1;
  padding: 8px;
  border: 1px solid #e0e0e0;
  border-radius: 5px;
  font-size: 14px;
  margin-right: 10px;
}

.comment-input:focus {
  outline: none;
  border-color: #409eff;
}

.comment-button {
  padding: 8px 15px;
  background-color: #67c23a;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s ease;
}

.comment-button:hover {
  background-color: #85ce61;
}
</style>